class_name character_dao

# 表名
const table_name := "character"


# 初始化操作
func init():
	# 生成表
	SqliteDb.create_table_name(table_name, Character.new())
	pass


# 字典转对象
static func dic_to_obj(dic: Dictionary) ->Character:
	var obj: Character = Character.new()
	obj.id = dic["id"]
	obj.name = dic["name"]
	obj.age = dic["age"]
	obj.sex = dic["sex"]
	obj.birthdate = dic["birthdate"]
	obj.max_hp = dic["max_hp"]
	obj.hp = dic["hp"]
	obj.max_mp = dic["max_mp"]
	obj.mp = dic["mp"]
	obj.physique = dic["physique"]
	obj.force = dic["force"]
	obj.speed = dic["speed"]
	obj.intelligence = dic["intelligence"]
	obj.spirit = dic["spirit"]
	obj.lucky = dic["lucky"]
	obj.charm = dic["charm"]
	obj.defense = dic["defense"]
	obj.mdef = dic["mdef"]
	obj.atk = dic["atk"]
	obj.lv = dic["lv"]
	obj.sum_exp = dic["sum_exp"]
	obj.exp = dic["exp"]
	return obj


# 根据id 查询数据
func query_by_id(id: int) ->Character:
	var sql := "id = %s" % ["id"]
	var arr: Array = SqliteDb.select_all(table_name, sql)
	if arr.size() > 0:
		return dic_to_obj(arr.front())
	return Character.new()


# 根据id修改数据
func update_by_id(character: Character):
	var dic: Dictionary = character.obj_to_dic()
	var condition = "id = %s" % [character.get_id()]
	SqliteDb.update(table_name, condition, dic)


# 查询玩家
func query_by_type()->Character:
	var sql := "type = %s" % [0]
	var arr: Array = SqliteDb.select_all(table_name, sql)
	if arr.size() > 0:
		return dic_to_obj(arr.front())
	return Character.new()


# 清空表
func delete_all_data():
	SqliteDb.delete_table_all_data(table_name)
	

